<?php
if( KF_EDITOR_MODE == 'Markdown' ) {
	include_once( Foundry::config('libPath').'/markdown.php' );
} else {
	include_once( Foundry::config('libPath').'/Textile.php' );
}

class net_mode3_news_newsController extends FoundryControllerInterface
{


	/**
	 * init()
	 *
	 * init() is called in place of the __construct() method used by the
	 * FoundryControllerInterface super class
	 * This function will be called immediately after the __construct method
	 *
	 */ 
	public function init()
	{
	
	
	}
	
	public function main()
	{
		
		$id = FoundryRequest::get( 'id', 'num' );
		
		$error = FoundryRequest::get( 'err', 'num' );

		$err = false;
		
		$allowModerate = false;
		
		if( $id ) {
			$post = FoundryController::model( 'net.mode3.news:news', $id );
			
			switch( $error ) {
				case 201:
					$err = 'failedChallenge';
					break;
			}
			
			if( $post->uid ) {
				if( $post->allow_comments ) {
					if( verifyAuth() && defined( 'IS_AUTHENTICATED' ) &&
						IS_AUTHENTICATED == 1 ) {
						$allowModerate = true;
						$post->comments();
					} else {
						$post->comments( true );
					}
				} else {
					$post->comments = array();
					
				}
				return array( 'post' => $post, 'error' => $err, 'allowModerate' => $allowModerate );
			}
		}
		
		return array( 'post' => FoundryController::model( 'net.mode3.news:news' ), 'error' => $err, 'allowModerate' => $allowModerate );
	}
	
	public function tag()
	{
		$id = FoundryRequest::get( 'id', 'alnum' );

		if( $id ) {
			$mNews = FoundryController::model( 'net.mode3.news:news' );
			$posts = $mNews->findByTag( $id );
			
			return array( 'posts' => $posts );
		}
		
		return array( 'posts' =>
			FoundryController::model( 'net.mode3.news:news' ) );

	}
	
	public function postComment()
	{
		$id = FoundryRequest::get( 'id', 'num' );
		$name = FoundryRequest::post( 'name', 'alnum' );
		$email = FoundryRequest::post( 'email', 'email' );
		$cmt = cleanString( FoundryRequest::post( 'comment', 'alnum' ) );

		$error = 0;

		if( $id && $cmt ) {
			$post = FoundryController::model( 'net.mode3.news:news', $id );
			
			$comment = FoundryController::model( 'net.mode3.news:comment' );
			
			$response = false;
			if( $post->challenge && $post->response ) {
				$response = $post->response;
			}
			if( $response === false ||
				$response == strtolower( FoundryRequest::post( 'response', 'alnum' ) ) ) {
				if( $post->uid ) {
					
					if( !$name ) {
						$name = 'Anon';
					}
					
					$comment->name = $name;
					$comment->email = $email;
					$comment->comment = $cmt;
					
					if( KF_EDITOR_MODE == 'Markdown' ) {
						$comment->comment_markdown = Markdown( $comment->comment );
					} else {
						$textile = new Textile;
						$cmt = preg_replace(
							"/&quot;([a-zA-Z0-9\-_ ]*)&quot;\:/",
							'"$1":',
							$comment->comment );
					
						$comment->comment_markdown = $textile->process( $cmt );
						
						$comment->net_mode3_news_new_id = $id;
						
						if( !$post->moderate ) {
							$post->comments_total++;
							$post->save();
							
							$comment->published = 1;
						}
						
						$comment->save();
						
					}
				}
			} else {
				$error = 201;
			}
		}

		header( "Location: ".FoundryView::helperLinkTo( 'news/main/'.$post->uid.'/'.$error ) );
		exit;

	}
	
	public function publishComment()
	{
		$id = FoundryRequest::get( 'id', 'num' );
		$comment = FoundryRequest::get( 'err', 'num' );
		
		if( $id && $comment && verifyAuth() && defined( 'IS_AUTHENTICATED' ) &&
			IS_AUTHENTICATED == 1 ) {

			$post = FoundryController::model( 'net.mode3.news:news', $id );
			$comment = FoundryController::model( 'net.mode3.news:comment', $comment );
		
			$post->comments_total++;
			$post->save();
			
			$comment->published = 1;
			
			$comment->save();
		}
		header( "Location: ".FoundryView::helperLinkTo( 'news/main/'.$post->uid ) );
		exit;
	
	}

}

?>